3
https://raw.githubusercontent.com/Silthus/sLimits/master/src/main/java/net/silthus/slib/config/typeconversions/TypeConversion.java
package net.silthus.slib.config.typeconversions;

import java.lang.reflect.Type;

/**
 * @author zml2008
 */
public abstract class TypeConversion implements Comparable<TypeConversion> {

    public Object handle(Class<?> target, Type[] neededGenerics, Object value) {

        if (isApplicable(target, value)
                && (getParametersRequired() == -1 || neededGenerics.length == getParametersRequired())) {
            return cast(target, neededGenerics, value);
        } else {
            return null;
        }
    }

    protected abstract Object cast(Class<?> target, Type[] neededGenerics, Object value);

    public abstract boolean isApplicable(Class<?> target, Object value);

    protected abstract int getParametersRequired();

    public int compareTo(TypeConversion other) {

        return Integer.valueOf(getParametersRequired()).compareTo(other.getParametersRequired());
    }
}
